


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > CollisionInteractionMap</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.level</a>
</div>

<h1>Coverage Summary for Class: CollisionInteractionMap (nl.tudelft.jpacman.level)</h1>

<table class="coverageStats">

<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">CollisionInteractionMap</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/7)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/38)
  </span>
</td>
</tr>
  <tr>
    <td class="name">CollisionInteractionMap$InverseCollisionHandler</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/2)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/3)
  </span>
</td>
  </tr>
<tr>
  <td class="name"><strong>Total</strong></td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/9)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/41)
  </span>
</td>
</tr>
</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.level;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import java.util.ArrayList;
<i class="no-highlight">4</i>&nbsp;import java.util.HashMap;
<i class="no-highlight">5</i>&nbsp;import java.util.List;
<i class="no-highlight">6</i>&nbsp;import java.util.Map;
<i class="no-highlight">7</i>&nbsp;
<i class="no-highlight">8</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
<i class="no-highlight">9</i>&nbsp;
<i class="no-highlight">10</i>&nbsp;/**
<i class="no-highlight">11</i>&nbsp; * A map of possible collisions and their handlers.
<i class="no-highlight">12</i>&nbsp; *
<i class="no-highlight">13</i>&nbsp; * @author Michael de Jong
<i class="no-highlight">14</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">15</i>&nbsp; */
<i class="no-highlight">16</i>&nbsp;public class CollisionInteractionMap implements CollisionMap {
<i class="no-highlight">17</i>&nbsp;
<i class="no-highlight">18</i>&nbsp;    /**
<i class="no-highlight">19</i>&nbsp;     * The collection of collision handlers.
<i class="no-highlight">20</i>&nbsp;     */
<i class="no-highlight">21</i>&nbsp;    private final Map&lt;Class&lt;? extends Unit&gt;,
<i class="no-highlight">22</i>&nbsp;        Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt;&gt; handlers;
<i class="no-highlight">23</i>&nbsp;
<i class="no-highlight">24</i>&nbsp;    /**
<i class="no-highlight">25</i>&nbsp;     * Creates a new, empty collision map.
<i class="no-highlight">26</i>&nbsp;     */
<b class="nc"><i class="no-highlight">27</i>&nbsp;    public CollisionInteractionMap() {</b>
<b class="nc"><i class="no-highlight">28</i>&nbsp;        this.handlers = new HashMap&lt;&gt;();</b>
<i class="no-highlight">29</i>&nbsp;    }
<i class="no-highlight">30</i>&nbsp;
<i class="no-highlight">31</i>&nbsp;    /**
<i class="no-highlight">32</i>&nbsp;     * Adds a two-way collision interaction to this collection, i.e. the
<i class="no-highlight">33</i>&nbsp;     * collision handler will be used for both C1 versus C2 and C2 versus C1.
<i class="no-highlight">34</i>&nbsp;     *
<i class="no-highlight">35</i>&nbsp;     * @param &lt;C1&gt;
<i class="no-highlight">36</i>&nbsp;     *            The collider type.
<i class="no-highlight">37</i>&nbsp;     * @param &lt;C2&gt;
<i class="no-highlight">38</i>&nbsp;     *            The collidee (unit that was moved into) type.
<i class="no-highlight">39</i>&nbsp;     *
<i class="no-highlight">40</i>&nbsp;     * @param collider
<i class="no-highlight">41</i>&nbsp;     *            The collider type.
<i class="no-highlight">42</i>&nbsp;     * @param collidee
<i class="no-highlight">43</i>&nbsp;     *            The collidee type.
<i class="no-highlight">44</i>&nbsp;     * @param handler
<i class="no-highlight">45</i>&nbsp;     *            The handler that handles the collision.
<i class="no-highlight">46</i>&nbsp;     */
<i class="no-highlight">47</i>&nbsp;    public &lt;C1 extends Unit, C2 extends Unit&gt; void onCollision(
<i class="no-highlight">48</i>&nbsp;        Class&lt;C1&gt; collider, Class&lt;C2&gt; collidee, CollisionHandler&lt;C1, C2&gt; handler) {
<b class="nc"><i class="no-highlight">49</i>&nbsp;        onCollision(collider, collidee, true, handler);</b>
<i class="no-highlight">50</i>&nbsp;    }
<i class="no-highlight">51</i>&nbsp;
<i class="no-highlight">52</i>&nbsp;    /**
<i class="no-highlight">53</i>&nbsp;     * Adds a collision interaction to this collection.
<i class="no-highlight">54</i>&nbsp;     *
<i class="no-highlight">55</i>&nbsp;     * @param &lt;C1&gt;
<i class="no-highlight">56</i>&nbsp;     *            The collider type.
<i class="no-highlight">57</i>&nbsp;     * @param &lt;C2&gt;
<i class="no-highlight">58</i>&nbsp;     *            The collidee (unit that was moved into) type.
<i class="no-highlight">59</i>&nbsp;     *
<i class="no-highlight">60</i>&nbsp;     * @param collider
<i class="no-highlight">61</i>&nbsp;     *            The collider type.
<i class="no-highlight">62</i>&nbsp;     * @param collidee
<i class="no-highlight">63</i>&nbsp;     *            The collidee type.
<i class="no-highlight">64</i>&nbsp;     * @param symetric
<i class="no-highlight">65</i>&nbsp;     *            &lt;code&gt;true&lt;/code&gt; if this collision is used for both
<i class="no-highlight">66</i>&nbsp;     *            C1 against C2 and vice versa;
<i class="no-highlight">67</i>&nbsp;     *            &lt;code&gt;false&lt;/code&gt; if only for C1 against C2.
<i class="no-highlight">68</i>&nbsp;     * @param handler
<i class="no-highlight">69</i>&nbsp;     *            The handler that handles the collision.
<i class="no-highlight">70</i>&nbsp;     */
<i class="no-highlight">71</i>&nbsp;    public &lt;C1 extends Unit, C2 extends Unit&gt; void onCollision(
<i class="no-highlight">72</i>&nbsp;        Class&lt;C1&gt; collider, Class&lt;C2&gt; collidee, boolean symetric,
<i class="no-highlight">73</i>&nbsp;        CollisionHandler&lt;C1, C2&gt; handler) {
<b class="nc"><i class="no-highlight">74</i>&nbsp;        addHandler(collider, collidee, handler);</b>
<b class="nc"><i class="no-highlight">75</i>&nbsp;        if (symetric) {</b>
<b class="nc"><i class="no-highlight">76</i>&nbsp;            addHandler(collidee, collider, new InverseCollisionHandler&lt;&gt;(handler));</b>
<i class="no-highlight">77</i>&nbsp;        }
<i class="no-highlight">78</i>&nbsp;    }
<i class="no-highlight">79</i>&nbsp;
<i class="no-highlight">80</i>&nbsp;    /**
<i class="no-highlight">81</i>&nbsp;     * Adds the collision interaction..
<i class="no-highlight">82</i>&nbsp;     *
<i class="no-highlight">83</i>&nbsp;     * @param collider
<i class="no-highlight">84</i>&nbsp;     *            The collider type.
<i class="no-highlight">85</i>&nbsp;     * @param collidee
<i class="no-highlight">86</i>&nbsp;     *            The collidee type.
<i class="no-highlight">87</i>&nbsp;     * @param handler
<i class="no-highlight">88</i>&nbsp;     *            The handler that handles the collision.
<i class="no-highlight">89</i>&nbsp;     */
<i class="no-highlight">90</i>&nbsp;    private void addHandler(Class&lt;? extends Unit&gt; collider,
<i class="no-highlight">91</i>&nbsp;                            Class&lt;? extends Unit&gt; collidee, CollisionHandler&lt;?, ?&gt; handler) {
<b class="nc"><i class="no-highlight">92</i>&nbsp;        if (!handlers.containsKey(collider)) {</b>
<b class="nc"><i class="no-highlight">93</i>&nbsp;            handlers.put(collider, new HashMap&lt;&gt;());</b>
<i class="no-highlight">94</i>&nbsp;        }
<i class="no-highlight">95</i>&nbsp;
<b class="nc"><i class="no-highlight">96</i>&nbsp;        Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt; map = handlers.get(collider);</b>
<b class="nc"><i class="no-highlight">97</i>&nbsp;        map.put(collidee, handler);</b>
<i class="no-highlight">98</i>&nbsp;    }
<i class="no-highlight">99</i>&nbsp;
<i class="no-highlight">100</i>&nbsp;    /**
<i class="no-highlight">101</i>&nbsp;     * Handles the collision between two colliding parties, if a suitable
<i class="no-highlight">102</i>&nbsp;     * collision handler is listed.
<i class="no-highlight">103</i>&nbsp;     *
<i class="no-highlight">104</i>&nbsp;     * @param &lt;C1&gt;
<i class="no-highlight">105</i>&nbsp;     *            The collider type.
<i class="no-highlight">106</i>&nbsp;     * @param &lt;C2&gt;
<i class="no-highlight">107</i>&nbsp;     *            The collidee (unit that was moved into) type.
<i class="no-highlight">108</i>&nbsp;     *
<i class="no-highlight">109</i>&nbsp;     * @param collider
<i class="no-highlight">110</i>&nbsp;     *            The collider.
<i class="no-highlight">111</i>&nbsp;     * @param collidee
<i class="no-highlight">112</i>&nbsp;     *            The collidee.
<i class="no-highlight">113</i>&nbsp;     */
<i class="no-highlight">114</i>&nbsp;    @SuppressWarnings(&quot;unchecked&quot;)
<i class="no-highlight">115</i>&nbsp;    @Override
<i class="no-highlight">116</i>&nbsp;    public &lt;C1 extends Unit, C2 extends Unit&gt; void collide(C1 collider,
<i class="no-highlight">117</i>&nbsp;                                                           C2 collidee) {
<b class="nc"><i class="no-highlight">118</i>&nbsp;        Class&lt;? extends Unit&gt; colliderKey = getMostSpecificClass(handlers, collider.getClass());</b>
<b class="nc"><i class="no-highlight">119</i>&nbsp;        if (colliderKey == null) {</b>
<i class="no-highlight">120</i>&nbsp;            return;
<i class="no-highlight">121</i>&nbsp;        }
<i class="no-highlight">122</i>&nbsp;
<b class="nc"><i class="no-highlight">123</i>&nbsp;        Map&lt;Class&lt;? extends Unit&gt;, CollisionHandler&lt;?, ?&gt;&gt; map = handlers.get(colliderKey);</b>
<b class="nc"><i class="no-highlight">124</i>&nbsp;        Class&lt;? extends Unit&gt; collideeKey = getMostSpecificClass(map, collidee.getClass());</b>
<b class="nc"><i class="no-highlight">125</i>&nbsp;        if (collideeKey == null) {</b>
<i class="no-highlight">126</i>&nbsp;            return;
<i class="no-highlight">127</i>&nbsp;        }
<i class="no-highlight">128</i>&nbsp;
<b class="nc"><i class="no-highlight">129</i>&nbsp;        CollisionHandler&lt;C1, C2&gt; collisionHandler = (CollisionHandler&lt;C1, C2&gt;) map.get(collideeKey);</b>
<b class="nc"><i class="no-highlight">130</i>&nbsp;        if (collisionHandler == null) {</b>
<i class="no-highlight">131</i>&nbsp;            return;
<i class="no-highlight">132</i>&nbsp;        }
<i class="no-highlight">133</i>&nbsp;
<b class="nc"><i class="no-highlight">134</i>&nbsp;        collisionHandler.handleCollision(collider, collidee);</b>
<i class="no-highlight">135</i>&nbsp;    }
<i class="no-highlight">136</i>&nbsp;
<i class="no-highlight">137</i>&nbsp;    /**
<i class="no-highlight">138</i>&nbsp;     * Figures out the most specific class that is listed in the map. I.e. if A
<i class="no-highlight">139</i>&nbsp;     * extends B and B is listed while requesting A, then B will be returned.
<i class="no-highlight">140</i>&nbsp;     *
<i class="no-highlight">141</i>&nbsp;     * @param map
<i class="no-highlight">142</i>&nbsp;     *            The map with the key collection to find a matching class in.
<i class="no-highlight">143</i>&nbsp;     * @param key
<i class="no-highlight">144</i>&nbsp;     *            The class to search the most suitable key for.
<i class="no-highlight">145</i>&nbsp;     * @return The most specific class from the key collection.
<i class="no-highlight">146</i>&nbsp;     */
<i class="no-highlight">147</i>&nbsp;    private Class&lt;? extends Unit&gt; getMostSpecificClass(
<i class="no-highlight">148</i>&nbsp;        Map&lt;Class&lt;? extends Unit&gt;, ?&gt; map, Class&lt;? extends Unit&gt; key) {
<b class="nc"><i class="no-highlight">149</i>&nbsp;        List&lt;Class&lt;? extends Unit&gt;&gt; collideeInheritance = getInheritance(key);</b>
<b class="nc"><i class="no-highlight">150</i>&nbsp;        for (Class&lt;? extends Unit&gt; pointer : collideeInheritance) {</b>
<b class="nc"><i class="no-highlight">151</i>&nbsp;            if (map.containsKey(pointer)) {</b>
<b class="nc"><i class="no-highlight">152</i>&nbsp;                return pointer;</b>
<i class="no-highlight">153</i>&nbsp;            }
<b class="nc"><i class="no-highlight">154</i>&nbsp;        }</b>
<b class="nc"><i class="no-highlight">155</i>&nbsp;        return null;</b>
<i class="no-highlight">156</i>&nbsp;    }
<i class="no-highlight">157</i>&nbsp;
<i class="no-highlight">158</i>&nbsp;    /**
<i class="no-highlight">159</i>&nbsp;     * Returns a list of all classes and interfaces the class inherits.
<i class="no-highlight">160</i>&nbsp;     *
<i class="no-highlight">161</i>&nbsp;     * @param clazz
<i class="no-highlight">162</i>&nbsp;     *            The class to create a list of super classes and interfaces
<i class="no-highlight">163</i>&nbsp;     *            for.
<i class="no-highlight">164</i>&nbsp;     * @return A list of all classes and interfaces the class inherits.
<i class="no-highlight">165</i>&nbsp;     */
<i class="no-highlight">166</i>&nbsp;    @SuppressWarnings(&quot;unchecked&quot;)
<i class="no-highlight">167</i>&nbsp;    private List&lt;Class&lt;? extends Unit&gt;&gt; getInheritance(
<i class="no-highlight">168</i>&nbsp;        Class&lt;? extends Unit&gt; clazz) {
<b class="nc"><i class="no-highlight">169</i>&nbsp;        List&lt;Class&lt;? extends Unit&gt;&gt; found = new ArrayList&lt;&gt;();</b>
<b class="nc"><i class="no-highlight">170</i>&nbsp;        found.add(clazz);</b>
<i class="no-highlight">171</i>&nbsp;
<b class="nc"><i class="no-highlight">172</i>&nbsp;        int index = 0;</b>
<b class="nc"><i class="no-highlight">173</i>&nbsp;        while (found.size() &gt; index) {</b>
<b class="nc"><i class="no-highlight">174</i>&nbsp;            Class&lt;?&gt; current = found.get(index);</b>
<b class="nc"><i class="no-highlight">175</i>&nbsp;            Class&lt;?&gt; superClass = current.getSuperclass();</b>
<b class="nc"><i class="no-highlight">176</i>&nbsp;            if (superClass != null &amp;&amp; Unit.class.isAssignableFrom(superClass)) {</b>
<b class="nc"><i class="no-highlight">177</i>&nbsp;                found.add((Class&lt;? extends Unit&gt;) superClass);</b>
<i class="no-highlight">178</i>&nbsp;            }
<b class="nc"><i class="no-highlight">179</i>&nbsp;            for (Class&lt;?&gt; classInterface : current.getInterfaces()) {</b>
<b class="nc"><i class="no-highlight">180</i>&nbsp;                if (Unit.class.isAssignableFrom(classInterface)) {</b>
<b class="nc"><i class="no-highlight">181</i>&nbsp;                    found.add((Class&lt;? extends Unit&gt;) classInterface);</b>
<i class="no-highlight">182</i>&nbsp;                }
<i class="no-highlight">183</i>&nbsp;            }
<b class="nc"><i class="no-highlight">184</i>&nbsp;            index++;</b>
<b class="nc"><i class="no-highlight">185</i>&nbsp;        }</b>
<i class="no-highlight">186</i>&nbsp;
<b class="nc"><i class="no-highlight">187</i>&nbsp;        return found;</b>
<i class="no-highlight">188</i>&nbsp;    }
<i class="no-highlight">189</i>&nbsp;
<i class="no-highlight">190</i>&nbsp;    /**
<i class="no-highlight">191</i>&nbsp;     * Handles the collision between two colliding parties.
<i class="no-highlight">192</i>&nbsp;     *
<i class="no-highlight">193</i>&nbsp;     * @author Michael de Jong
<i class="no-highlight">194</i>&nbsp;     *
<i class="no-highlight">195</i>&nbsp;     * @param &lt;C1&gt;
<i class="no-highlight">196</i>&nbsp;     *            The collider type.
<i class="no-highlight">197</i>&nbsp;     * @param &lt;C2&gt;
<i class="no-highlight">198</i>&nbsp;     *            The collidee type.
<i class="no-highlight">199</i>&nbsp;     */
<i class="no-highlight">200</i>&nbsp;    public interface CollisionHandler&lt;C1 extends Unit, C2 extends Unit&gt; {
<i class="no-highlight">201</i>&nbsp;
<i class="no-highlight">202</i>&nbsp;        /**
<i class="no-highlight">203</i>&nbsp;         * Handles the collision between two colliding parties.
<i class="no-highlight">204</i>&nbsp;         *
<i class="no-highlight">205</i>&nbsp;         * @param collider
<i class="no-highlight">206</i>&nbsp;         *            The collider.
<i class="no-highlight">207</i>&nbsp;         * @param collidee
<i class="no-highlight">208</i>&nbsp;         *            The collidee.
<i class="no-highlight">209</i>&nbsp;         */
<i class="no-highlight">210</i>&nbsp;        void handleCollision(C1 collider, C2 collidee);
<i class="no-highlight">211</i>&nbsp;    }
<i class="no-highlight">212</i>&nbsp;
<i class="no-highlight">213</i>&nbsp;    /**
<i class="no-highlight">214</i>&nbsp;     * An symmetrical copy of a collision hander.
<i class="no-highlight">215</i>&nbsp;     *
<i class="no-highlight">216</i>&nbsp;     * @author Michael de Jong
<i class="no-highlight">217</i>&nbsp;     *
<i class="no-highlight">218</i>&nbsp;     * @param &lt;C1&gt;
<i class="no-highlight">219</i>&nbsp;     *            The collider type.
<i class="no-highlight">220</i>&nbsp;     * @param &lt;C2&gt;
<i class="no-highlight">221</i>&nbsp;     *            The collidee type.
<i class="no-highlight">222</i>&nbsp;     */
<i class="no-highlight">223</i>&nbsp;    private static class InverseCollisionHandler&lt;C1 extends Unit, C2 extends Unit&gt;
<i class="no-highlight">224</i>&nbsp;        implements CollisionHandler&lt;C1, C2&gt; {
<i class="no-highlight">225</i>&nbsp;
<i class="no-highlight">226</i>&nbsp;        /**
<i class="no-highlight">227</i>&nbsp;         * The handler of this collision.
<i class="no-highlight">228</i>&nbsp;         */
<i class="no-highlight">229</i>&nbsp;        private final CollisionHandler&lt;C2, C1&gt; handler;
<i class="no-highlight">230</i>&nbsp;
<i class="no-highlight">231</i>&nbsp;        /**
<i class="no-highlight">232</i>&nbsp;         * Creates a new collision handler.
<i class="no-highlight">233</i>&nbsp;         *
<i class="no-highlight">234</i>&nbsp;         * @param handler
<i class="no-highlight">235</i>&nbsp;         *            The symmetric handler for this collision.
<i class="no-highlight">236</i>&nbsp;         */
<b class="nc"><i class="no-highlight">237</i>&nbsp;        InverseCollisionHandler(CollisionHandler&lt;C2, C1&gt; handler) {</b>
<b class="nc"><i class="no-highlight">238</i>&nbsp;            this.handler = handler;</b>
<i class="no-highlight">239</i>&nbsp;        }
<i class="no-highlight">240</i>&nbsp;
<i class="no-highlight">241</i>&nbsp;        /**
<i class="no-highlight">242</i>&nbsp;         * Handles this collision by flipping the collider and collidee, making
<i class="no-highlight">243</i>&nbsp;         * it compatible with the initial collision.
<i class="no-highlight">244</i>&nbsp;         */
<i class="no-highlight">245</i>&nbsp;        @Override
<i class="no-highlight">246</i>&nbsp;        public void handleCollision(C1 collider, C2 collidee) {
<b class="nc"><i class="no-highlight">247</i>&nbsp;            handler.handleCollision(collidee, collider);</b>
<i class="no-highlight">248</i>&nbsp;        }
<i class="no-highlight">249</i>&nbsp;    }
<i class="no-highlight">250</i>&nbsp;
<i class="no-highlight">251</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-16 18:35</div>
</div>
</body>
</html>
